const Mock = require('mockjs');

const { Random } = Mock;

const IDGen = (function*(start) {
  while (true) {
    yield ++start;
  }
})(1000);

const loginUser = {
  account: '7949',
  password: 'handhand',
  userName: 'StandBy',
  gender: '1',
  location: 'HAND',
  age: 29,
};

const userList = [];

const limitList = [];

for (let i = 0; i < 25; i++) {
  const userName = Random.cname();
  userList.push({
    id: `ID${IDGen.next().value}`,
    userName,
    gender: '' + Random.integer(0, 1),
    location: `${Random.province()}-${Random.city()}-${Random.county()}`,
    description: Random.sentence(20),
    birthday: Random.datetime(),
    age: Random.integer(10, 60),
    createDate: new Date().getTime(),
  });
}

for (let i = 0; i < 25; i++) {
  const userName = Random.cname();
  limitList.push({
    id: `ID${IDGen.next().value}`,
    userName,
    gender: '' + Random.integer(0, 1),
    location: `${Random.province()}-${Random.city()}-${Random.county()}`,
    description: Random.sentence(20),
    birthday: Random.datetime(),
    age: Random.integer(10, 60),
    createDate: new Date().getTime(),
  });
}

module.exports = {
  name: 'user',
  desc: 'user js apis',
  apis: [
    {
      name: 'user login',
      desc: 'user login',
      method: 'POST',
      url: '/user/login',
      handle: (req, res) => {
        const loginData = req.body;

        const { account, password } = loginData[0];

        res.status(200);
        setTimeout(() => {
          if (
            loginUser.account === account &&
            loginUser.password === password
          ) {
            const user = Object.assign({}, loginUser);
            delete user.password;
            res.send({
              success: true,
              data: user,
            });
          } else {
            res.send({
              success: false,
              data: '账号或密码错误',
            });
          }
        }, 1500);
      },
    },
    {
      name: 'user list',
      desc: 'user list',
      method: 'POST',
      url: '/user/list',
      handle: (req, res) => {
        const page = req.body.page;
        const pageSize = req.body.pageSize;
        const userName = req.body.userName;
        const gender = req.body.gender;

        res.status(200);

        const allDatas = userList
          .sort((a, b) => {
            return b.createDate - a.createDate;
          })
          .filter((user) => {
            if (userName || gender) {
              if (userName && !gender) {
                return user.userName.indexOf(userName) > -1;
              } else if (!userName && gender) {
                return user.gender === gender;
              } else if (userName && gender) {
                return (
                  user.userName.indexOf(userName) > -1 && user.gender === gender
                );
              }
            } else {
              return true;
            }
          });

        let data = [];

        for (let i = 0; i < pageSize; i++) {
          const user = allDatas[i + (page - 1) * pageSize];
          if (user) {
            data.push({
              id: user.id,
              userName: user.userName,
              gender: user.gender,
            });
          }
        }

        setTimeout(() => {
          res.send({
            success: true,
            total: allDatas.length,
            data: data,
          });
        }, 1500);
      },
    },
    {
      name: 'user detail',
      desc: 'user detail',
      method: 'POST',
      url: '/user/detail',
      handle: (req, res) => {
        const id = req.body.id;

        res.status(200);

        const user = limitList.filter((user) => {
          return user.id === id;
        });

        setTimeout(() => {
          res.send({
            success: true,
            total: user.length,
            data: user,
          });
        }, 1500);
      },
    },
    {
      name: 'user delete',
      desc: 'user delete',
      method: 'POST',
      url: '/user/delete',
      handle: (req, res) => {
        const records = req.body;

        const ret = [];

        records.forEach((record) => {
          const id = record.id;
          const currIndex = userList.findIndex((re) => {
            return re.id === id;
          });

          userList.splice(currIndex, 1);
        });

        res.status(200);

        setTimeout(() => {
          res.send({
            success: true,
            total: ret.length,
            data: ret,
          });
        }, 1500);
      },
    },
    {
      name: 'user update',
      desc: 'user update',
      method: 'POST',
      url: '/user/update',
      handle: (req, res) => {
        const records = req.body;

        const ret = [];

        records.forEach((record) => {
          const id = record.id;
          const currIndex = limitList.findIndex((re) => {
            return re.id === id;
          });

          delete record.status;
          limitList[currIndex] = record;
          ret.push(record);
        });

        res.status(200);

        setTimeout(() => {
          res.send({
            success: true,
            total: ret.length,
            data: ret,
          });
        }, 1200);
      },
    },
    {
      name: 'user submit',
      desc: 'user submit',
      method: 'POST',
      url: '/user/submit',
      handle: (req, res) => {
        const records = req.body;

        const ret = [];

        records.forEach((record) => {
          const id = record.id;
          const currIndex = limitList.findIndex((re) => {
            return re.id === id;
          });

          const status = record.status;
          switch (status) {
            case 'deleted':
              limitList.splice(currIndex, 1);
              break;
            case 'created':
              record.id = `ID${IDGen.next().value}`;
              record.createDate = new Date().getTime();
              delete record.status;
              limitList.push(record);
              ret.push(record);
              break;
            case 'updated':
              delete record.status;
              limitList[currIndex] = record;
              ret.push(record);
              break;
          }
        });

        res.status(200);

        setTimeout(() => {
          res.send({
            success: true,
            total: ret.length,
            data: ret,
          });
        }, 1500);
      },
    },
    {
      name: 'user limit list',
      desc: 'user limit list',
      method: 'POST',
      url: '/user/limit-list',
      handle: (req, res) => {
        res.status(200);

        setTimeout(() => {
          res.send({
            success: true,
            total: limitList.length,
            data: limitList,
          });
        }, 1500);
      },
    },
  ],
};
